//
//  PullCollectionViewCell.m
//  瀑布流封装
//
//  Created by 常宽 on 14/11/1.
//  Copyright (c) 2014年 常宽. All rights reserved.
//

#import "PullCollectionViewCell.h"
#import "CKCar.h"

@implementation PullCollectionViewCell

#pragma mark 自定义cell里不能添加点击事件

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
//        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"边框.png"]];
        self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"biankuang.jpg"]];
        self.imageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_imageView];
        [_imageView release];
        
        self.title = [[UILabel alloc]init];
        [self.contentView addSubview:_title];
        [_title release];
        
    }
    return self;
}


- (void)dealloc
{
    [_imageView release];
    [_title release];
    [_car release];
    [super dealloc];
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    [self setupSubviews];
}
- (void)setupSubviews
{
    
    UIImage *aImage = _imageView.image;
    
    CGFloat w = self.contentView.frame.size.width;
    CGFloat h = self.contentView.frame.size.height;
    
    CGFloat offW = aImage.size.width / w;
    CGFloat offH = 1;
    if (aImage != nil) {
        offH = aImage.size.height / offW;
    }
    _imageView.frame = CGRectMake(2.5, 0, w, offH);
//    [_imageView setBackgroundColor:[UIColor redColor]];
    _imageView.clipsToBounds = YES;
    _imageView.contentMode = UIViewContentModeScaleAspectFill;
    
    self.title.numberOfLines = 0;// 可以显示无限行
    self.title.textAlignment = NSTextAlignmentCenter;
    UIFont *font = [UIFont systemFontOfSize:17.f];
    self.title.font = font;
    
    self.title.frame = CGRectMake(2.5, offH + 5, w, h - offH);
    
//    [self.title setBackgroundColor:[UIColor grayColor]];
    [self.title sizeToFit]; // label自适应文本大小
    
}

- (void)setCar:(CKCar *)car
{
    if (_car != car) {
        [_car release];
        _car = [car retain];
    }
    [_imageView sd_setImageWithURL:[NSURL URLWithString:_car.image]];
    if (_imageView.image == nil) {
        _imageView.image = [UIImage imageNamed:@"NoPicture.png"];
    }
    self.title.text = _car.title;
}


@end
